package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SignsArrayAdapter extends ArrayAdapter<SignsPojo> {
	Activity context = null;
	int layoutId;
	ArrayList<SignsPojo> arr = null;

	// Contructor nÃ y dÃ¹ng Ä‘á»ƒ láº¥y vá»� nhá»¯ng giÃ¡ trá»‹ Ä‘Æ°á»£c
	// truyá»�n vÃ o tá»« MainActivity
	public SignsArrayAdapter(Activity context, int layoutId,
			ArrayList<SignsPojo> list) {
		super(context, layoutId, list);

		this.context = context;
		this.layoutId = layoutId;
		this.arr = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/*
		 * position: lÃ  vá»‹ trÃ­ cá»§a biá»ƒn bÃ¡o trong list convertView:
		 * dÃ¹ng Ä‘á»ƒ láº¥y vá»� cÃ¡c control cá»§a má»—i item parent: chÃ­nh
		 * lÃ  datasource Ä‘Æ°á»£c truyá»�n vÃ o tá»« MainActivity
		 */

		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = inflater.inflate(layoutId, null);
		}

		// Láº¥y vá»� biá»ƒn bÃ¡o á»Ÿ vá»‹ trÃ­ dc yÃªu cáº§u
		SignsPojo signs = arr.get(position);

		// Láº¥y ra nhá»¯ng control Ä‘Æ°á»£c Ä‘á»‹nh nghÄ©a trong cáº¥u trÃºc
		// cá»§a má»—i item
		ImageView icon = (ImageView) convertView.findViewById(R.id.list_image);
		TextView title = (TextView) convertView.findViewById(R.id.title);
		TextView group = (TextView) convertView.findViewById(R.id.group);

		// GÃ¡n giÃ¡ trá»‹ cho control Ä‘Ã³
		title.setText(signs.getTitle());
		group.setText(signs.getGroup());

		// VÃ¬ icon lÃ  áº£nh nÃªn ta pháº£i láº¥y ra Ä‘Æ°á»�ng dáº«n, dÃ¹ng nÃ³
		// Ä‘á»ƒ láº¥y vá»� image trong folder drawable
		String uri_icon = "drawable/" + signs.getIcon();
		int ImageResoure = convertView
				.getContext()
				.getResources()
				.getIdentifier(
						uri_icon,
						null,
						convertView.getContext().getApplicationContext()
								.getPackageName());
		Drawable image = convertView.getContext().getResources()
				.getDrawable(ImageResoure);
		icon.setImageDrawable(image);

		return convertView;
	}
}
